package gui;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Process;
import model.ProcessType;
import service.Service;

public class AddEditProcess extends JFrame {
    private final Controller controller = new Controller();
    private Component owner;
    private JTextField txfName, txfMinH, txfMinM, txfIdealH, txfIdealM,
	    txfMaxH, txfMaxM;
    private JCheckBox chckbxRunsInStock;
    private JButton btnSave, btnCancel;
    private Process process = null;

    public AddEditProcess(Component owner, Process proc) {
	this(owner);
	process = proc;
	fillData();
    }

    public AddEditProcess(Component owner) {
	this.owner = owner;
	setSize(239, 244);
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setLocation(100, 100);
	setResizable(false);
	setVisible(true);
	setTitle("Add/Edit process");
	getContentPane().setLayout(new GridLayout(6, 0, 0, 0));

	JPanel panel = new JPanel();
	FlowLayout flowLayout = (FlowLayout) panel.getLayout();
	flowLayout.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel);

	JLabel lblName = new JLabel("Name");
	panel.add(lblName);

	txfName = new JTextField();
	panel.add(txfName);
	txfName.setColumns(10);

	JPanel panel_1 = new JPanel();
	FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
	flowLayout_1.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel_1);

	JLabel lblMinimum = new JLabel("Minimum");
	panel_1.add(lblMinimum);

	txfMinH = new JTextField("0");
	panel_1.add(txfMinH);
	txfMinH.setColumns(3);

	JLabel lblHours = new JLabel("hours");
	panel_1.add(lblHours);

	txfMinM = new JTextField("0");
	panel_1.add(txfMinM);
	txfMinM.setColumns(3);

	JLabel lblMinutes = new JLabel("minutes");
	panel_1.add(lblMinutes);

	JPanel panel_2 = new JPanel();
	FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
	flowLayout_2.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel_2);

	JLabel lblIdeally = new JLabel("Ideally");
	panel_2.add(lblIdeally);

	txfIdealH = new JTextField("0");
	panel_2.add(txfIdealH);
	txfIdealH.setColumns(3);

	JLabel lblHours_1 = new JLabel("hours");
	panel_2.add(lblHours_1);

	txfIdealM = new JTextField("0");
	panel_2.add(txfIdealM);
	txfIdealM.setColumns(3);

	JLabel lblMinutes_1 = new JLabel("minutes");
	panel_2.add(lblMinutes_1);

	JPanel panel_3 = new JPanel();
	FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
	flowLayout_3.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel_3);

	JLabel lblMaximum = new JLabel("Maximum");
	panel_3.add(lblMaximum);

	txfMaxH = new JTextField("0");
	panel_3.add(txfMaxH);
	txfMaxH.setColumns(3);

	JLabel lblHours_2 = new JLabel("hours");
	panel_3.add(lblHours_2);

	txfMaxM = new JTextField("0");
	panel_3.add(txfMaxM);
	txfMaxM.setColumns(3);

	JLabel lblMinutes_2 = new JLabel("minutes");
	panel_3.add(lblMinutes_2);

	JPanel panel_4 = new JPanel();
	FlowLayout flowLayout_4 = (FlowLayout) panel_4.getLayout();
	flowLayout_4.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel_4);

	chckbxRunsInStock = new JCheckBox("Runs in stock area");
	panel_4.add(chckbxRunsInStock);

	JPanel panel_5 = new JPanel();
	FlowLayout flowLayout_5 = (FlowLayout) panel_5.getLayout();
	flowLayout_5.setAlignment(FlowLayout.RIGHT);
	getContentPane().add(panel_5);

	btnSave = new JButton("Save");
	panel_5.add(btnSave);

	btnCancel = new JButton("Cancel");
	panel_5.add(btnCancel);

	btnSave.addActionListener(controller);
	btnCancel.addActionListener(controller);
    }

    public void fillData() {
	txfName.setText(process.getName());

	txfMinH.setText(Service.getInstance()
		.milisToHours(process.getMinTime()) + "");
	txfMinM.setText(Service.getInstance().milisToMins(process.getMinTime())
		+ "");

	txfIdealH.setText(Service.getInstance().milisToHours(
		process.getIdealTime())
		+ "");
	txfIdealM.setText(Service.getInstance().milisToMins(
		process.getIdealTime())
		+ "");

	txfMaxH.setText(Service.getInstance()
		.milisToHours(process.getMaxTime()) + "");
	txfMaxM.setText(Service.getInstance().milisToMins(process.getMaxTime())
		+ "");

	chckbxRunsInStock
		.setSelected(process.getProcessType() == ProcessType.IN_STOCK);
    }

    private class Controller implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
	    if (e.getSource() == btnSave) {
		try {
		    String name = txfName.getText();
		    int minH = Integer.parseInt(txfMinH.getText());
		    int minM = Integer.parseInt(txfMinM.getText());
		    int idealH = Integer.parseInt(txfIdealH.getText());
		    int idealM = Integer.parseInt(txfIdealM.getText());
		    int maxH = Integer.parseInt(txfMaxH.getText());
		    int maxM = Integer.parseInt(txfMaxM.getText());

		    ProcessType type = ProcessType.NOT_IN_STOCK;

		    if (chckbxRunsInStock.isSelected()) {
			type = ProcessType.IN_STOCK;
		    }

		    long min = Service.getInstance().convertToMilis(minH, minM);
		    long ideal = Service.getInstance().convertToMilis(idealH,
			    idealM);
		    long max = Service.getInstance().convertToMilis(maxH, maxM);

		    Process p;

		    if (process == null)
			p = Service.getInstance().addProcess(name, min, ideal,
				max, type);
		    else
			p = Service.getInstance().editProcess(process, name,
				min, ideal, max, type);

		    if (p != null) {
			AddEditProcess.this.dispose();
		    } else {
			JOptionPane
				.showMessageDialog(
					AddEditProcess.this,
					"There was an error, process was not created/edited",
					"Error", JOptionPane.ERROR_MESSAGE);
		    }
		} catch (NumberFormatException ex) {
		    JOptionPane.showMessageDialog(AddEditProcess.this,
			    "Please enter valid numbers", "Error",
			    JOptionPane.ERROR_MESSAGE);
		}
		if (owner instanceof ManageData) {
		    ((ManageData) owner).updateLists();
		}
	    } else if (e.getSource() == btnCancel) {
		AddEditProcess.this.dispose();
	    }
	}

    }
}
